import React, { useEffect, useState } from 'react';
import { Row, Col, message } from 'antd';
import InterfaceInfo from '../../../InterfaceManage/Detail/interfaceInfo';
import DataBaseInterfaceInfo from '../../../databaseInterfaceManage/Detail/interfaceInfo';
import MainInterfaceInfo from '../../../mainSpareInterface/Detail/interfaceInfo';
import services from '../../../../../../services/api';

const VersionDetail = ({ record }) => {
  const [data, setData] = useState({});
  useEffect(() => {
    getSnapshotDetail();
  }, []);

  const getSnapshotDetail = async () => {
    const { data, code, msg } = await services.history.detail(record.id);
    if (code) return message.error(msg);
    const { thirdInterfaceList, returningMaps } = data;
    // 构造数据源接口的map
    const interfaceMap = {};
    thirdInterfaceList.forEach((item) => (interfaceMap[item.id] = [item.name, item.supplierName]));
    data['interfaceMap'] = interfaceMap;
    // 构造返回值映射
    const tempObj = {};
    returningMaps &&
      returningMaps.forEach((item) => {
        const { innerPath } = item;
        if (!tempObj[innerPath]) {
          tempObj[innerPath] = {
            thirdInterfaceList: [item],
            ...item,
          };
        } else {
          tempObj[innerPath].thirdInterfaceList.push(item);
        }
      });
    data['returningMap'] = Object.values(tempObj);
    setData(data);
  };

  const DetailComponent = [InterfaceInfo, DataBaseInterfaceInfo, MainInterfaceInfo, MainInterfaceInfo][
    data.dataSourceTypeValue
  ];

  return (
    <>
      <Row>
        <Col span={6}>版本号：</Col>
        <Col span={18}>{record.version}</Col>
      </Row>
      {data.dataSourceTypeValue !== undefined && <DetailComponent value={data} isVersion />}
    </>
  );
};

export default VersionDetail;
